<?php
	include_once("DataProvider.php");
	include_once("../DTO/cauHinhSPDTO.php");
	include_once("../DTO/sanPhamDTO.php");
	
	class sanPhamDAO
	{
		public $sql = "";
		var $sanPhamDTO;
		function __construct()
		{
			return $this->sanPhamDTO = new sanPhamDTO();
		}	
		
		public function load()
		{
			$result= DataProvider::ExecuteQuery($this->sql);
			$arrSanPhamDTO = new ArrayObject();
			
			while($row = mysql_fetch_array($result))
			{
				$sanPhamDTO = new sanPhamDTO();
				$sanPhamDTO->idSP = $row['idSP'];
				$sanPhamDTO->idHangSX = $row['idHangSX'];
				$sanPhamDTO->idLoaiSP = $row['idLoaiSP'];
				$sanPhamDTO->idTaiKhoan = $row['idTaiKhoan'];
				$sanPhamDTO->tenSP = $row['tenSP'];
				$sanPhamDTO->giaHienTai = $row['giaHienTai'];
				$sanPhamDTO->ngay = $row['ngay'];
				$sanPhamDTO->hinhURL = $row['hinhURL'];
				$sanPhamDTO->soLuongTon = $row['soLuongTon'];
				$sanPhamDTO->soLuongBan = $row['soLuongBan'];
				$sanPhamDTO->soLuongXem = $row['soLuongXem'];
				$sanPhamDTO->biXoa = $row['biXoa'];
				
				$arrSanPhamDTO[] = $sanPhamDTO;
			}
			return $arrSanPhamDTO;
		}
		
		public function load_SP_All()
		{
			$this->sql= 'select * from sanpham';
			$result=$this->load();
			return $result; 
		}
		
		public function load_SP_One($idSP)
		{
			$this->sql = 'select * from sanPham where idSP ='.$idSP;
			$result = $this->load();
			return $result;
		}
		
		public function load_SP()
		{	
			$this->sql = 'select * from sanPham where biXoa=0';
			$result = $this->load();
			return $result;
		}
		
		public function load_SP_Hot()
		{	
			$this->sql = 'select * from sanPham where biXoa=0 order by soLuongBan DESC limit 0,10 ';
			$result = $this->load();
			return $result;
		}
		
		function themSanPham()
		{
			$idTaiKhoan = $_SESSION["id"];
			$tenSP = $_POST['txtTenSanPham'];
            $idHangSX = $_POST['cmbHangSX'];
            $idLoaiSP = $_POST['cmbLoaiSP'];
            $gia = $_POST['txtGia'];
            $soLuong = $_POST['txtSoLuong'];
			$ngay = $currentDate = date("Y-m-d");            
            $sql1 = "insert into sanPham(idSP, idHangSX, idLoaiSP, idTaiKhoan, tenSP, giaHienTai, ngay, hinhURL, soLuongTon, soLuongBan, soLuongXem, biXoa) values(NULL, '$idHangSX', '$idLoaiSP','$idTaiKhoan', '$tenSP', '$gia', $ngay, NULL, '$soLuong', 0, 0, 0)";			
            $result1 = DataProvider::ExecuteQuery($sql1);
			$this->sql = 'select * from sanPham where biXoa=0 order by idSP DESC limit 0,1';			
			$result = $this->load();
            return $result;
		}
		
		
	}
	
?>